package com.hbyj.struts.action.authority;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import org.apache.struts2.ServletActionContext;
import com.hbyj.db.function.DbTools;

import com.hbyj.hbm.vo.Functions;

import com.hbyj.object.ZTreeNode;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class AuthorityFunctionAction extends ActionSupport {
	private String jsonResult;
	
	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}
	
	public void function_update() throws UnsupportedEncodingException, IOException{
		try
		{
		HttpServletRequest request = ServletActionContext.getRequest();
		String name = request.getParameter("name");
		String id = request.getParameter("id");
		int idNum = Integer.parseInt(id);
		
		Functions function = new Functions();
		function = (Functions)DbTools.findByProperty("Functions", "id", idNum).get(0);
		function.setName(name);
		Boolean rlt = DbTools.saveOrUpdateObj(function);
		ServletActionContext.getResponse().getOutputStream().write((rlt.toString()).getBytes("utf-8"));
		}
		catch (Exception e){
			e.printStackTrace();
			ServletActionContext.getResponse().getOutputStream().write((e.toString()).getBytes("utf-8"));
		}
	}
	
	public void function_delete() throws UnsupportedEncodingException, IOException{
		try
		{
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		int idNum = Integer.parseInt(id);
		
		Functions function = new Functions();
		function = (Functions)DbTools.findByProperty("Functions", "id", idNum).get(0);
		Boolean rlt=DbTools.deleteObj(function);
		 
		ServletActionContext.getResponse().getOutputStream().write((rlt.toString()).getBytes("utf-8"));
		}
		catch (Exception e){
			e.printStackTrace();
			ServletActionContext.getResponse().getOutputStream().write((e.toString()).getBytes("utf-8"));
		}
	}
	
	public void function_add() throws UnsupportedEncodingException, IOException{
		try
		{
		HttpServletRequest request = ServletActionContext.getRequest();
		String level = request.getParameter("level");
		String name = request.getParameter("name");
		String pid = request.getParameter("pid");
		
		int pidNum = Integer.parseInt(pid);
		int levelNum = Integer.parseInt(level);
		
		Functions function = new Functions(null,name,levelNum,pidNum);
		Boolean rlt = DbTools.saveObj(function);
		ServletActionContext.getResponse().getOutputStream().write((rlt.toString()).getBytes("utf-8"));
		}
		catch (Exception e){
			e.printStackTrace();
			ServletActionContext.getResponse().getOutputStream().write((e.toString()).getBytes("utf-8"));
		}
	}
	
	public void function_query() throws UnsupportedEncodingException, IOException{
		List functionsList = DbTools.findByHql("from Functions");
		List<ZTreeNode> list = new ArrayList<ZTreeNode>();
		
		for (Object nodeObj : functionsList) {
			Functions node = (Functions)nodeObj;
			ZTreeNode node2 = new ZTreeNode(node);
			list.add(node2);
		}
		
	    String jsonText = JSONArray.fromObject(list).toString();
		 ServletActionContext.getResponse().getOutputStream().write(jsonText.getBytes("utf-8"));
	}
}
